package io.github.tangwan.designpattern.creational.singleton;

/**
 * 双检锁/双重校验锁（DCL，即 double-checked locking）
 * 懒汉-线程安全-性能提高
 * @author tangwan
 * @version V1.0
 * created on 2021/9/27 11:23
 * @since JDK 1.8
 */
public class Singleton4 {

    //内存可见性修饰
    private volatile static Singleton4 singleton;

    private Singleton4() {
    }

    //双重检查
    public static Singleton4 getSingleton() {
        if (singleton == null) {
            synchronized (Singleton4.class) {
                if (singleton == null) {
                    singleton = new Singleton4();
                }
            }
        }
        return singleton;
    }
}
